package com.zxg.zbc.web.common.util;

import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.taobao.api.internal.util.WebUtils;
import com.zxg.util.WebUtil;

public class ScrapeEleme {

	
	public static void main(String[] args) {
		String latitude = "";
		String longitude = "";
		int offset = 0;
		int limit = 30;
		double startx = 121.20;//上海 120.95 bj 116.26 hz 120.01
		double endx = 121.69;//上海 121.69 bj 116.53 hz120.26
		double starty = 30.82;//sh 30.82 bj 39.78 hz 30.10
		double endy = 31.37;//sh 31.37 bj40.00 hz 30.45
		String url="https://mainsite-restapi.ele.me/shopping/restaurants?latitude="+latitude+"&longitude="+longitude+"&offset="+offset+"&limit="+limit+"&extras[]=activities&terminal=h5";
		try {
			PrintWriter pw  = new PrintWriter( new FileWriter("/Users/hangding/elmsh0901-12.txt"));
			for(double i = startx;i<=endx;i=i+0.02){
				for(double y=starty;y<=endy;y=y+0.02){
					if(i==121.20){
						if(y<=30.97)
							continue;
					}
					System.out.println("process "+i+" "+y);
					processShops(y+"55", i+"55", pw);
				}
			}
			processShops(endy+"55", endx+"55", pw);
			//System.out.println(parseInfo("https://mainsite-restapi.ele.me/shopping/restaurants?latitude=30.289583&longitude=120.19318&offset=30&limit=30&extras[]=activities&terminal=h5",pw));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	static List<String> ids = new ArrayList<String>();
	public static void processShops(String latitude,String longitude,PrintWriter pw){
		
		int limit = 30;
		for(int i=0;i<=30;i++){
			int offset = i*limit;
			String url="https://mainsite-restapi.ele.me/shopping/restaurants?latitude="+latitude+"&longitude="+longitude+"&offset="+offset+"&limit="+limit+"&extras[]=activities&terminal=h5";
			try {
				List<String> ss = parseInfo(url, pw);
				if(ss==null || ss.size()<=0){
					System.out.println("end process "+latitude+" "+longitude);
					break;
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	public static List<String> parseInfo(String url,PrintWriter pw ) throws IOException{
		List<String> rtn = new ArrayList<String>();
		try {
			Thread.sleep(1000l);
		} catch (InterruptedException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		String content = WebUtils.doGet(url, null);
		try{
			List<JSONObject> shops = JSONArray.parseArray(content, JSONObject.class);
			if(shops!=null && shops.size()>0){
				for(JSONObject obj:shops){
					StringBuffer sb = new StringBuffer();
					String id = obj.getString("id");
					if(ids.contains(id))
						continue;
					ids.add(id);
					sb.append(obj.getString("name")).append("`")
					.append(obj.getString("id")).append("`")
					.append(obj.getString("name")).append("`")
					.append(obj.getString("description")).append("`")
					.append(obj.getString("address")).append("`")
					.append(obj.getString("opening_hours")).append("`")
					.append(obj.getString("phone")).append("`")
					.append(obj.getString("piecewise_agent_fee")).append("`")
					.append(obj.getString("promotion_info")).append("`")
					.append(obj.getString("rating")).append("`")
					.append(obj.getString("rating_count")).append("`")
					.append(obj.getString("recent_order_num")).append("`")
					.append(obj.getString("id")).append("`");
					rtn.add(sb.toString());
					pw.println(sb.toString());
					pw.flush();
				}
			}
			System.out.println(ids.size());
			return rtn;
		}catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		
	}
}
